package com.netbliz.common.action;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;



import com.netbliz.common.util.PageBean;
import com.netbliz.ftpmanagment.util.SessionUtil;
import com.opensymphony.xwork2.ActionSupport;
/**
 * ACTION基类，所有ACTION类均继承此类
 * @author jyxiao
 *
 */
@SuppressWarnings("all")
public abstract class BaseAction extends ActionSupport{
	private static org.apache.log4j.Logger logger=Logger.getLogger(BaseAction.class);
	/**
	 * 请求数据
	 */
	protected String requestData;
	/**
	 * 响应数据
	 */
	protected String responseData;
	/**
	 * 分页BEAN
	 */
	protected PageBean pageBean;
	protected int page;
	protected int pageSize;
	public String getResponseData() {
		return responseData;
	}
	public void setResponseData(String responseData) {
		this.responseData = responseData;
	}
	public String getRequestData() {
		return requestData;
	}
	public void setRequestData(String requestData) {
		this.requestData = requestData;
	}
	
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		if(page<1){
			page=1;
		}
		this.page = page;
	}
	public int getPageSize() {
		if(pageSize<1){
			pageSize=8;
		}
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public PageBean getPageBean() {
		return pageBean;
	}
	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}
	/**
	 * 异步请求时，将响应数据以JSON格式写入 response
	 */
	protected boolean writeJson(String result){
		HttpServletResponse response=SessionUtil.getHttpServletResponse();
		try {
			response.setContentType("text/json;charset=UTF-8;");
			response.getWriter().write(result);
		} catch (IOException e) {
			logger.error("写入JSON响应数据出错，响应数据为："+result+"\n"+"出错信息："+e.getMessage());
			return false;
		}
		return true;
	}
	/**
	 * 异步请求时，将响应数据以HTML格式写入 response
	 */
	protected boolean writeHtml(String result){
		HttpServletResponse response=SessionUtil.getHttpServletResponse();
		try {
			response.setContentType("text/html;charset=UTF-8;");
			response.getWriter().write(result);
		} catch (IOException e) {
			logger.error("写入HTML响应数据出错，响应数据为："+result+"\n"+"出错信息："+e.getMessage());
			return false;
		}
		return true;
	}
	/**
	 * 将请求数据写入JSONOBJECT
	 * @return
	 */
	protected JSONObject getJsonObjectByRequestData(){
		if(null==requestData || requestData.trim().equals("")){
			return null;
		}
		JSONObject jRequestObject=null;
		try {
			jRequestObject=new JSONObject(requestData);
		} catch (JSONException e) {
			logger.error("将请求数据转换为JSON OBJECT出错，请求数据为："+requestData+"\n"+"出错信息："+e.getMessage());
		}
		return jRequestObject;
	}
	
	
	protected HttpServletRequest request = null;

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
}
